﻿using Microsoft.Extensions.Configuration;

namespace config1
{
    internal class Program
    {
        //入门
        static void Main(string[] args)
        {
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.AddJsonFile("config.json", optional: true, reloadOnChange: true);
            IConfigurationRoot config = configurationBuilder.Build();
            //1、获取单个某个节点
            string type = config["type"];
            Console.WriteLine($"type={type}");
            string logLevel = config.GetSection("log:level").Value;
            Console.WriteLine($"logLevel={logLevel}");
            //2、可以可以绑定一个类，自动完成配置的读取
            //单个小节点
            Log logs = config.GetSection("log").Get<Log>();
            Console.WriteLine($"logtype={logs.type}");
            //根节点
            Config configS = config.Get<Config>();
            Console.WriteLine($"database={configS.database}");
            Console.WriteLine($"logLevel1={configS.Log.level}");
            Console.ReadKey();
        }

        class Config
        {
            public string type { get; set; }
            public string hostname { get; set; }
            public string database { get; set; }
            public string password { get; set; }
            public string hostport { get; set; }
            public Log Log { get; set; }
        }

        class Log
        {
            public string type { set; get; }
            public string level { set; get; }
        }
    }
}